package org.me;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.util.StringUtils;
import org.me.utils.Koneksi;

import android.app.Dialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class AddUser extends Dialog implements android.view.View.OnClickListener, OnItemSelectedListener {

	ContactList contactList;
	Client client;
	String[] group;
	String[] selectedGroup;
	String selected;

	public AddUser(ContactList contactList) {
		super(contactList);
		this.contactList = contactList;
	}
	
	public AddUser(Client client) {
		super(client);
		this.client = client;
	}
	
	protected void onStart() {
		super.onStart();
        setContentView(R.layout.add_contact_dialog);
        getWindow().setFlags(4, 4);
        
        ArrayList<String> groups = Koneksi.getInstance().getAllGroup();
        Set<String> s = new LinkedHashSet<String>(groups);
        ArrayList<String> temp = new ArrayList<String>(s);
        group = new String[temp.size()];

        for (int i=0; i<group.length; i++) {
        	group[i] = temp.get(i);
		}
        for (int i=0; i<group.length; i++) {
        	System.out.println(s.toString());
		}
        
        
        Spinner group_spinner = (Spinner) findViewById(R.id.group_spinner);
        group_spinner.setOnItemSelectedListener(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
		        getContext(),
		        android.R.layout.simple_spinner_item,
		        group);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		group_spinner.setAdapter(adapter);
        
        Button set = (Button) findViewById(R.id.btnAdd);
        set.setOnClickListener(this);
        Button cancelAdd = (Button) findViewById(R.id.cancelAdd);
        cancelAdd.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnAdd:
			EditText add = (EditText) findViewById(R.id.textAdd);
			EditText addgroup = (EditText) findViewById(R.id.new_group);
			
			String server = StringUtils.parseServer(Koneksi.getInstance().getConnection().getUser());
			String addUser = add.getText().toString()+"@"+server;
			String newGroups = addgroup.getText().toString();
			
			Roster roster = Koneksi.getInstance().getConnection().getRoster();
			if (newGroups.equalsIgnoreCase("") || newGroups == null) {
				try {
					roster.createEntry(addUser, add.getText().toString(), selectedGroup );
				} catch (XMPPException e) {
					e.printStackTrace();
				}
			}
			else {
				roster.createGroup(newGroups);
				try {
					roster.createEntry(addUser, add.getText().toString(), new String[] {newGroups});
				} catch (XMPPException e) {
					e.printStackTrace();
				}
			}
			
	        dismiss();
			break;
		case R.id.cancelAdd:
			dismiss();
			break;

		default:
			dismiss();
			break;
		}
		
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		// TODO Auto-generated method stub
		selected = group[pos];
		System.out.println(selected);
		selectedGroup = new String[1];
		selectedGroup[0] = selected;
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

}
